//
//  RHDeviceToPoFullScreenVCViewController.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/4/25.
//

#import "RHDeviceToPoFullScreenVC.h"
#import "RHDeviceTopoHEBATView.h"
#import "RHDeviceTopoMEFullView.h"
#import "RHTopoModel.h"
#import "RHDeviceTopoHEBATParallelView.h"
@interface RHDeviceToPoFullScreenVC ()

@property (nonatomic ,strong) RHDeviceModel *devModel;

@property (nonatomic ,strong) RHTopoModel *topoModel;

@property (nonatomic, strong) UIView *topoBgView;

@property (nonatomic, strong) RHDeviceTopoHEBATView *HE_topoView;

//HE_BAT 并机
@property (nonatomic, strong) RHDeviceTopoHEBATParallelView *HE_BATParallelView;

@property (nonatomic, strong) RHDeviceTopoMEFullView *ME_topoView;

@property (nonatomic, copy) NSString *emuSN;
@end

@implementation RHDeviceToPoFullScreenVC
-(instancetype)initWithDevModel:(RHDeviceModel*)devModel topoModel:(RHTopoModel*)topoModel emuSN:(NSString*)sn
{
    if(self = [super init]){
        self.devModel = devModel;
        self.topoModel = topoModel;
        self.emuSN = sn;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.devModel.productTypeId == ProductType_HE_BAT||self.devModel.productTypeId == ProductType_HE_ALLINONE) {
            [self getDeviceInfo];
        }
       
        if ([self.devModel.deviceTypeCode is233DeviceType]) {
            [self getDeviceTopo233];
        }
        
        if ([self.devModel.deviceTypeCode is61DeviceType]) {
            [self getDeviceInfo];
        }
    });
    [self setupUI];
}


- (void)setupUI{
    kWeakSelf;
    self.view.backgroundColor = UIColor.whiteColor;
    
    _topoBgView = kView;
    _topoBgView.backgroundColor = [UIColor whiteColor];
    _topoBgView.layer.cornerRadius = 8;
    _topoBgView.layer.masksToBounds = YES;
    [self.bgView addSubview:_topoBgView];
    [_topoBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
//        make.height.offset(kUI_WINDOW_W*0.48+30);
    }];
 
    
    if(self.devModel.productTypeId == ProductType_HE_BAT||self.devModel.productTypeId == ProductType_HE_ALLINONE){
        
        _HE_BATParallelView = [[RHDeviceTopoHEBATParallelView alloc]initWithOrientation:YES];
        [_topoBgView addSubview:_HE_BATParallelView];
        [_HE_BATParallelView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_offset(0);
            make.top.offset(30);
        }];

    }
    if(self.devModel.productTypeId == ProductType_ME)
    {
        if ([self.devModel.deviceTypeCode is61DeviceType]) {//工商储61度电
            _HE_BATParallelView = [[RHDeviceTopoHEBATParallelView alloc]initWithOrientation:YES];
            _HE_BATParallelView.layer.cornerRadius = 8;
            _HE_BATParallelView.layer.masksToBounds = YES;
            [_topoBgView addSubview:_HE_BATParallelView];
            [_HE_BATParallelView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.mas_offset(0);
                make.top.offset(30);
            }];
        }else{
            _ME_topoView = [[RHDeviceTopoMEFullView alloc]initWithDeviceModel:self.devModel];
            [_topoBgView addSubview:_ME_topoView];
            [_ME_topoView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.mas_offset(0);
                make.top.offset(30);
            }];
        }
    }
    
    
    QMUIButton *fullBtn = [[QMUIButton alloc]init];
    if(self.devModel.productTypeId == ProductType_HE_BAT||self.devModel.productTypeId == ProductType_HE_ALLINONE){
        [fullBtn setImage:[UIImage imageNamed:@"full_back"] forState:UIControlStateNormal];
        [_topoBgView addSubview:fullBtn];
        [fullBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.offset(35);
            make.top.mas_offset(15);
            make.right.mas_offset(-10);
        }];
    }
    
    
    if(self.devModel.productTypeId == ProductType_ME)
    {
        [self.view addSubview:fullBtn];
        [fullBtn setImage:[UIImage imageNamed:@"back_icon"] forState:UIControlStateNormal];
        [fullBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.height.offset(35);
            make.top.mas_offset(15);
            make.left.mas_offset(25);
        }];
    }
    
    [[fullBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
           delegate.allowRotate = 0;
        if(@available(iOS 16.0, *)) {
            [weakSelf.navigationController setNeedsUpdateOfSupportedInterfaceOrientations];
            NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
            UIWindowScene *ws = (UIWindowScene *)array[0];
            UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] init];
            geometryPreferences.interfaceOrientations = UIInterfaceOrientationMaskPortrait;
            [ws requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:^(NSError * _Nonnull error) {
                 //业务代码
            }];
        }else{
            NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
            [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
        }
        [weakSelf.navigationController popViewControllerAnimated:YES];
      
    }];

}


#pragma mark ------ 获取设备信息(topo数据)
-(void)getDeviceInfo
{
    [QMUITips showLoadingInView:self.view];
    NSString *url = [NSString stringWithFormat:@"%@%ld",H_getDeviceInfo,self.devModel.device_id];
    [RHTopoModel startGetWithCompletionAPIKey:url argument:@{} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if(resultModel.code == 200){
            RHTopoModel *topoModel = resultModel.data;
         
            //并机
            [self.HE_BATParallelView setTopoModel:topoModel deviceModel:self.devModel];
           
            
        }
        
        } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
                
        }];
}


#pragma mark ------ 获取设备topo信息(233k)
-(void)getDeviceTopo233
{
    [QMUITips showLoadingInView:self.view];
    NSString *url = [NSString stringWithFormat:@"%@%ld",H_getDeviceTopoInfo233,self.devModel.device_id];
    [RH233TopoModel startGetWithCompletionAPIKey:url argument:@{} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if(resultModel.code == 200){
            [self.ME_topoView setTopo233Model:resultModel.data];
        }
        
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
            
    }];
    
}

@end
